<template>
    <div class="p-2">
        <div class="mb-[10px]" v-show="showSearch">
            <el-card shadow="hover">
                <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="100">
                    <el-form-item label="企业名称" prop="name">
                        <el-input v-model="queryParams.name" placeholder="请输入企业名称" clearable style="width: 200px"
                            @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-form-item label="成立日期" prop="foundationDate">
                        <el-date-picker v-model="queryParams.foundationDate" value-format="YYYY-MM-DD" type="date"
                            placeholder="请选择成立日期" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-card>
        </div>
        <el-card shadow="hover">
            <template #header>
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="Plus" @click="handleAdd"
                            v-hasPermi="permissionList.add">新增</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                            v-hasPermi="permissionList.edit">修改</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                            v-hasPermi="permissionList.remove">
                            删除
                        </el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="warning" plain icon="Download" @click="handleExport"
                            v-hasPermi="permissionList.export">导出</el-button>
                    </el-col>
                    <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>
            </template>
            <el-table v-loading="loading" :data="regisiterList" border @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="企业名称" align="center" prop="name" min-width="140" />
                <el-table-column label="法定代表人" align="center" prop="legalRepresentative" min-width="200"
                    show-overflow-tooltip />
                <el-table-column label="注册资金" align="center" prop="capital" min-width="180" />
                <el-table-column label="成立日期" align="center" prop="foundationDate" min-width="180" />
                <el-table-column label="注册所在地" align="center" prop="address" min-width="180" show-overflow-tooltip />
                <el-table-column label="统一社会信用码" align="center" prop="registrationNumber" min-width="180" />
                <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width"
                    fixed="right">
                    <template #default="scope">
                        <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                            v-hasPermi="permissionList.edit">修改</el-button>
                        <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                            v-hasPermi="permissionList.remove">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <!-- 添加或修改企业对话框 -->
        <el-dialog :title="dialog.title" v-model="dialog.visible" width="600px" append-to-body>
            <el-form ref="postFormRef" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="企业名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入企业名称" />
                </el-form-item>
                <el-form-item label="法定代表人" prop="legalRepresentative">
                    <el-input v-model="form.legalRepresentative" placeholder="请输入法定代表人" />
                </el-form-item>
                <el-form-item label="注册时间" prop="foundationDate">
                    <el-date-picker v-model="form.foundationDate" value-format="YYYY-MM-DD" type="date"
                        placeholder="请选择注册时间" />
                </el-form-item>
                <el-form-item label="注册资金" prop="capital">
                    <el-input v-model="form.capital" placeholder="请输入注册资金" />
                </el-form-item>
                <el-form-item label="统一社会信用码" prop="registrationNumber">
                    <el-input v-model="form.registrationNumber" placeholder="请输入统一社会信用码" />
                </el-form-item>
                <el-form-item label="公司所在地" prop="address">
                    <el-input v-model="form.address" placeholder="请输入公司所在地" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="certificationRegisiter">
import { Plus } from '@element-plus/icons-vue'
import { list, add, del, update, getDetail } from "@/api/business/enterprise/enterpriseList.js";
import { ElMessage, ElMessageBox } from 'element-plus'
import { toRefs, ref, reactive, onMounted } from "vue";
const { proxy } = getCurrentInstance();
const pageName = "企业列表"
// 按钮权限
const permissionList = {
    add: ['business:certification:register:add'],
    edit: ['business:certification:register:edit'],
    remove: ['business:certification:register:remove'],
    export: ['business:certification:register:export']
};
// 中标数据
const regisiterList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const postFormRef = ref();
const queryFormRef = ref();
const dialog = reactive({
    visible: false,
    title: ''
});

const initFormData = {
    idArray: undefined,
    name: '',
    legalRepresentative: '',
    foundationDate: '',
    capital: "",
    registrationNumber: "",
    address: "",
}

const data = reactive({
    form: { ...initFormData },
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: '',
        foundationDate: '',
    },
    rules: {
        name: [{ required: true, message: "企业名称不能为空", trigger: "blur" }],
        legalRepresentative: [{ required: true, message: "法定代表人不能为空", trigger: "blur" }],
        foundationDate: [{ required: true, message: "注册时间不能为空", trigger: "change" }],
        capital: [{ required: true, message: "注册资金不能为空", trigger: "change" }],
        registrationNumber: [{ required: true, message: "统一社会信用码不能为空", trigger: "change" }],
        address: [{ required: true, message: "公司所在地不能为空", trigger: "change" }],
    }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公司列表 */
const getList = async () => {
    loading.value = true;
    const res = await list(queryParams.value);
    regisiterList.value = res.rows;
    total.value = res.total;
    loading.value = false;
}
/** 取消按钮 */
const cancel = () => {
    reset();
    dialog.visible = false;
}
/** 表单重置 */
const reset = () => {
    form.value = { ...initFormData };
    postFormRef.value?.resetFields();
}
/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
}
/** 多选框选中数据 */
const handleSelectionChange = (selection) => {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}
/** 新增按钮操作 */
const handleAdd = () => {
    reset();
    dialog.visible = true;
    dialog.title = "添加企业信息";
}
/** 修改按钮操作 */
const handleUpdate = async (row) => {
    reset();
    form.value.idArray = row?.id || ids.value[0];
    const res = await getDetail(form.value.idArray);
    Object.assign(form.value, res.data);
    dialog.visible = true;
    dialog.title = "修改企业信息";
}
/** 提交按钮 */
const submitForm = () => {
    postFormRef.value?.validate(async (valid) => {
        if (valid) {
            form.value.idArray ? await update(form.value) : await add(form.value);
            dialog.visible = false;
            ElMessage({
                type: 'success',
                message: '操作成功',
            })
            await getList();
        }
    });
}
/** 删除按钮操作 */
const handleDelete = async (row) => {
    ElMessageBox.confirm(
        '确定删除吗?',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            const idArray = row?.id || ids.value;
            del(idArray).then(() => {
                ElMessage({
                    type: 'success',
                    message: '删除成功',
                })
                getList();
            })
        })
        .catch(() => {
        })

}
/** 导出按钮操作 */
const handleExport = () => {
    proxy?.download("/system/companyCert/export", {
        ...queryParams.value
    }, `${pageName}${new Date().getTime()}.xlsx`);
}

onMounted(() => {
    getList();
});
</script>